Skip to content

feat: added csv functionalities in gyroscope screen. #2818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 16, 2025

Conversation

Yugesh-Kumar-S
Copy link
Contributor

@Yugesh-Kumar-S Yugesh-Kumar-S commented Aug 5, 2025

Fixes #2817

Changes

  • added csv functionalities to gyroscope screen
  • adapted logged data chart screen to support gyroscope and accelerometer data.

Screenshots / Recordings

screen-20250807-194558.mp4

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

Summary by Sourcery

Add CSV recording and export capabilities to the gyroscope screen, connect it to the logged data viewer, and enhance charting and storage handling for recorded sensor data

New Features:

  • Allow users to start/stop recording gyroscope data and export it as a CSV file via a save dialog
  • Add navigation from the gyroscope screen to a logged data viewer

Enhancements:

  • Refactor GyroscopeProvider to manage recording state and accumulate timestamped sensor readings
  • Improve LoggedDataChartScreen with axis selection for multi-axis instruments, dynamic Y-axis scaling, and formatted time labels
  • Update CSV service to write metadata, save files in the external storage directory, and remove manual permission requests

Chores:

  • Remove MANAGE_EXTERNAL_STORAGE permission from Android manifest

Copy link
Contributor

sourcery-ai bot commented Aug 5, 2025

Reviewer's Guide

This pull request adds CSV recording and export capabilities to the gyroscope screen with user-prompted filenames, refactors the GyroscopeProvider lifecycle and CsvService storage logic, removes manual permission requests, enhances the logged data chart with relative time and dynamic formatting, and cleans up related manifest permissions.

Entity relationship diagram for gyroscope CSV data structure

erDiagram
    GYROSCOPE_CSV {
      string Timestamp
      string DateTime
      double ReadingsX
      double ReadingsY
      double ReadingsZ
      double Latitude
      double Longitude
    }
Loading

Class diagram for updated GyroscopeProvider and CsvService

classDiagram
    class GyroscopeProvider {
      - StreamSubscription<GyroscopeEvent>? _gyroscopeSubscription
      - GyroscopeEvent _gyroscopeEvent
      - List<double> _xData
      - List<double> _yData
      - List<double> _zData
      - double _xMin, _xMax, _yMin, _yMax, _zMin, _zMax
      - bool _isRecording
      - List<List<dynamic>> _recordedData
      + double get xValue
      + double get yValue
      + double get zValue
      + bool get isListening
      + bool get isRecording
      + void initializeSensors()
      + void disposeSensors()
      + void startRecording()
      + List<List<dynamic>> stopRecording()
      + List<FlSpot> getAxisData(String axis)
    }

    class CsvService {
      + Future<Directory> getInstrumentDirectory(String instrumentName)
      + Future<File?> saveCsvFile(String instrumentName, String fileName, List<List<dynamic>> data)
      + Future<void> writeMetaData(String instrumentName, List<List<dynamic>> data)
    }

    GyroscopeProvider --> CsvService : uses for CSV export
Loading

Class diagram for updated LoggedDataChartScreen

classDiagram
    class LoggedDataChartScreen {
      + String fileName
      + List<List<dynamic>> data
      + String xAxisLabel
      + String yAxisLabel
      + int xDataColumnIndex
      + int yDataColumnIndex
      + String? instrumentName
      + Widget build(BuildContext context)
      + Widget _buildChart(...)
      + Widget _sideTitleWidgets(...)
    }

    LoggedDataChartScreen o-- "1" LoggedDataScreen : navigated from
    LoggedDataChartScreen *-- "1" GyroscopeProvider : uses data
Loading

File-Level Changes

Change Details Files
Add CSV recording and export controls to gyroscope screen UI
  • Inject CsvService and initialize GyroscopeProvider in initState/dispose
  • Implement _toggleRecording with SnackBar and save dialog
  • Show save-file AlertDialog prompting for filename and invoke CsvService
  • Pass onRecordPressed and isRecording to CommonScaffold
  • Add navigation to LoggedDataScreen via options menu
lib/view/gyroscope_screen.dart
Refactor CsvService to use app-specific external storage directory
  • Remove manual permission requests and PermissionHandler import
  • Use getExternalStorageDirectory() to construct instrument directory path
lib/others/csv_service.dart
Extend GyroscopeProvider to support data recording
  • Add _isRecording flag and _recordedData list
  • Record timestamp, formatted date, and sensor readings in listener
  • Implement startRecording() and stopRecording() to manage recordings
lib/providers/gyroscope_state_provider.dart
Enhance LoggedDataChartScreen with relative time and dynamic axis formatting
  • Compute relative time from first timestamp and adjust min/max/y intervals
  • Introduce _getSafeYInterval and refactor axis label formatting
  • Add instrument-specific axis selector for gyroscope/accelerometer
  • Extract side title widget builder for time units
lib/view/logged_data_chart_screen.dart
Propagate instrumentName to logged data screens
  • Add instrumentName parameter to LoggedDataChartScreen constructor
  • Pass widget.instrumentName in navigation calls in LoggedDataScreen
lib/view/logged_data_screen.dart
Remove unnecessary MANAGE_EXTERNAL_STORAGE permission
  • Delete MANAGE_EXTERNAL_STORAGE entry from AndroidManifest
android/app/src/main/AndroidManifest.xml

Assessment against linked issues

Issue Objective Addressed Explanation
#2817 Add CSV functionalities in gyroscope instrument using the csv_service.dart.
#2817 Adapt the logged_data_chart_screen.dart to display gyroscope data.

Possibly linked issues

  • #0: The PR adds CSV export and recording for gyroscope data, and updates the chart screen to properly display it, directly addressing the issue's requirements.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

github-actions bot commented Aug 5, 2025

@Yugesh-Kumar-S Yugesh-Kumar-S marked this pull request as ready for review August 6, 2025 17:31
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Yugesh-Kumar-S - I've reviewed your changes - here's some feedback:

  • Provide a meaningful default filename (e.g. “gyroscope_YYYYMMDD_HHMMSS”) in the save dialog so users don’t have to start with an empty string.
  • Extract the chart‐building logic in LoggedDataChartScreen (axis selector, side title widget, interval calculations) into smaller helper methods or widgets to improve readability.
  • Instead of manually instantiating and disposing GyroscopeProvider in initState/dispose, consider wrapping the screen in a ChangeNotifierProvider factory to let provider handle lifecycle automatically.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Provide a meaningful default filename (e.g. “gyroscope_YYYYMMDD_HHMMSS”) in the save dialog so users don’t have to start with an empty string.
- Extract the chart‐building logic in LoggedDataChartScreen (axis selector, side title widget, interval calculations) into smaller helper methods or widgets to improve readability.
- Instead of manually instantiating and disposing GyroscopeProvider in initState/dispose, consider wrapping the screen in a ChangeNotifierProvider factory to let provider handle lifecycle automatically.

## Individual Comments

### Comment 1
<location> `lib/view/logged_data_chart_screen.dart:221` </location>
<code_context>
     double minX = 0;
+    double? startTime;

     for (int i = 1; i < widget.data.length; i++) {
       final row = widget.data[i];
       if (row.length > widget.xDataColumnIndex &&
           row.length > widget.yDataColumnIndex) {
         final xValue = _parseDouble(row[widget.xDataColumnIndex]);
-        final yValue = _parseDouble(row[widget.yDataColumnIndex]);
+        final yValue = _parseDouble(row[_getYDataColumnIndex()]);

         if (xValue != null && yValue != null) {
-          spots.add(FlSpot(xValue, yValue));
+          if (startTime == null) {
+            startTime = xValue;
+            minX = 0;
+          }
+
+          final relativeTime = ((xValue - startTime) / 1000.0);
+
+          spots.add(FlSpot(relativeTime, yValue));
</code_context>

<issue_to_address>
Assumes xValue is in milliseconds for time axis calculation.

If the data source or CSV format changes, this assumption may cause incorrect time calculations. Please validate or document the expected time unit for xValue.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
          if (startTime == null) {
            startTime = xValue;
            minX = 0;
          }

          final relativeTime = ((xValue - startTime) / 1000.0);

          spots.add(FlSpot(relativeTime, yValue));
=======
          if (startTime == null) {
            startTime = xValue;
            minX = 0;
          }

          // Assumes xValue is in milliseconds. If the data source or CSV format changes,
          // this calculation may be incorrect. Please ensure xValue is in milliseconds.
          assert(xValue >= 0 && startTime >= 0, 'xValue and startTime should be non-negative and in milliseconds.');

          final relativeTime = ((xValue - startTime) / 1000.0);

          spots.add(FlSpot(relativeTime, yValue));
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 229 to 236
if (startTime == null) {
startTime = xValue;
minX = 0;
}

final relativeTime = ((xValue - startTime) / 1000.0);

spots.add(FlSpot(relativeTime, yValue));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Assumes xValue is in milliseconds for time axis calculation.

If the data source or CSV format changes, this assumption may cause incorrect time calculations. Please validate or document the expected time unit for xValue.

Suggested change
if (startTime == null) {
startTime = xValue;
minX = 0;
}
final relativeTime = ((xValue - startTime) / 1000.0);
spots.add(FlSpot(relativeTime, yValue));
if (startTime == null) {
startTime = xValue;
minX = 0;
}
// Assumes xValue is in milliseconds. If the data source or CSV format changes,
// this calculation may be incorrect. Please ensure xValue is in milliseconds.
assert(xValue >= 0 && startTime >= 0, 'xValue and startTime should be non-negative and in milliseconds.');
final relativeTime = ((xValue - startTime) / 1000.0);
spots.add(FlSpot(relativeTime, yValue));

@marcnause
Copy link
Contributor

🤦‍♂️ You are right! I got so many mails from Github that I was confused. Don't ask... 😉 Will fix the problem later today.

@AsCress
Copy link
Collaborator

AsCress commented Aug 16, 2025

@marcnause I guess you replied on the wrong pull request 😂

@marcnause marcnause merged commit 023870b into fossasia:flutter Aug 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add csv functionalities to gyroscope.
3 participants